package com.anolesoft.yr.mbus.contm.ctrl;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.redis.service.RedisService;
import com.anolesoft.yr.mbus.contm.service.ContmStyleMainService;
import com.anolesoft.yr.mbus.contm.entity.ContmStyleMain;
import com.anolesoft.yr.mbus.contm.service.StyleInsertService;
import com.anolesoft.yr.mbus.contm.util.FileUtils;
import com.anolesoft.yr.store.stk.util.StringUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/contm")
public class ContmStyleMainController {

    public ContmStyleMainController(StyleInsertService styleInsertService, RedisService redisService, ContmStyleMainService contmStyleMainService) {
        this.styleInsertService = styleInsertService;
        this.redisService = redisService;
        this.contmStyleMainService = contmStyleMainService;
    }

    private StyleInsertService styleInsertService;
    private RedisService redisService;
    private ContmStyleMainService contmStyleMainService;



    @GetMapping(value = "/contm_style_mian/get", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(ContmStyleMain contmStyleMain){

       // Object cacheObject = redisService.getCacheObject("login_user_key*");
        return contmStyleMainService.get(contmStyleMain);

    }

    @PostMapping(value = "/contm_style_mian/del", produces = {"application/json;charset=UTF-8"})
    public AjaxResult del(String objuid){

        // Object cacheObject = redisService.getCacheObject("login_user_key*");
        return contmStyleMainService.del(objuid);

    }

    @PostMapping(value = "/contm_style_mian/delbatch", produces = {"application/json;charset=UTF-8"})
    public AjaxResult delBatch(@RequestBody List<ContmStyleMain> contmStyleMains){



        // Object cacheObject = redisService.getCacheObject("login_user_key*");
        return contmStyleMainService.delBatch(contmStyleMains);

    }
    @Transactional
    @PostMapping(value = "contm_style_main/add" ,produces = {"application/json;charset=UTF-8"})
    public AjaxResult add(@RequestParam("file") MultipartFile file,@RequestParam("styleNameAdd")String styleNameAdd,@RequestParam("remark")String remark){
        String fileName = StringUtil.generateRundom32String();
        FileUtils.upload("e://temp",fileName+".docx",file);
        styleInsertService.docStyleInsertMain(StrUtil.concat(true, "e://temp", fileName+".docx"),styleNameAdd,remark);

        return AjaxResult.success("上传成功");
    }
    @GetMapping(value = "/contm_style_mian/getById", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(String objuid){
        // Object cacheObject = redisService.getCacheObject("login_user_key*");
        return contmStyleMainService.getById(objuid);
    }

    @PostMapping(value = "contm_style_main/edit" ,produces = {"application/json;charset=UTF-8"})
    public AjaxResult edit(@RequestParam("styleNameEdit")String styleNameEdit,@RequestParam("remark")String remark,@RequestParam("objuid")String objuid){
        styleInsertService.docStyleMainEdit(styleNameEdit,remark,objuid);
        return AjaxResult.success("修改成功");
    }

    @RequestMapping(value = "contm_style_main/selectList" ,produces = {"application/json;charset=UTF-8"})
    public List<ContmStyleMain> selectList(){
        List<ContmStyleMain> contmStyleMains = contmStyleMainService.selectList();
        return contmStyleMains;
    }

}
